package com.tzxs.pay.main.onlineaut;

import android.app.Activity;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.tzxs.pay.BR;
import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivityAutsResultBinding;
import com.tzxs.pay.databinding.ItemOnlineAutBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.main.bill.viewmodel.BillViewModel;
import com.tzxs.pay.main.onlineaut.model.OnlineAutBean;
import com.tzxs.pay.util.IAdapter;
import com.tzxs.pay.util.IHolder;

import java.util.List;

//在线预授权列表搜索结果
public class OnlineAutSListActivity extends BaseActivity<ActivityAutsResultBinding> implements IAdapter.ItemNavigator<OnlineAutBean.DataBean> {

    private String code = "";
    private String orderId = "";
    private String name = "";
    private String phone = "";
    private String orderPhone = "";
    private String startTime = "";
    private String endTime = "";

    private BillViewModel model;

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_auts_result;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIntent();
        model = ViewModelProviders.of(this).get(BillViewModel.class);
        dataBinding.setViewModel(model);
        dataBinding.resultToolbar.setNavigationOnClickListener(v -> onBackPressed());


        dataBinding.searchRec.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.searchRec.setAdapter(new IAdapter<OnlineAutBean.DataBean, ItemOnlineAutBinding>() {


            @Override
            protected int getLayoutId(int viewType) {
                return R.layout.item_online_aut;
            }


            @Override
            protected int getDataBRId(int itemViewType) {
                return BR.item;
            }


            @Override
            protected void convertListener(@NonNull ItemOnlineAutBinding binding) {
                binding.setItemNavigator(OnlineAutSListActivity.this);
            }

            @Override
            public void onBindViewHolder(IHolder<ItemOnlineAutBinding> holder, int position) {
                super.onBindViewHolder(holder, position);
                OnlineAutBean.DataBean bean = getData().get(position);
                switch (bean.getPay_status()) {
                    case 1:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#1F78CF"));
                        break;
                    case 2:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#999999"));
                        break;
                    case 3:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#27AA30"));
                        break;
                    case 4:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#ED433F"));
                        break;
                    case 5:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#EB9205"));
                        break;
                }
                holder.binding.payStatus.setText(bean.getStatusTypeText());
            }

            @Override
            public boolean areItemsTheSame(List<OnlineAutBean.DataBean> oldData, List<OnlineAutBean.DataBean> data, int oldItemPosition, int newItemPosition) {
                return oldData.get(oldItemPosition).getOut_order_no().equals(data.get(newItemPosition).getOut_order_no());
            }
        });

        //获取列表
        model.getSearchAutsList(code, orderId, name, phone, orderPhone, startTime, endTime, () -> {
            //获取数据成功
        });

    }

    //初始化
    private void initIntent() {
        code = getIntent().getStringExtra("code");
        orderId = getIntent().getStringExtra("orderId");
        name = getIntent().getStringExtra("name");
        phone = getIntent().getStringExtra("phone");
        orderPhone = getIntent().getStringExtra("orderPhone");
        startTime = getIntent().getStringExtra("startTime");
        endTime = getIntent().getStringExtra("endTime");
    }


    @Override
    public void itemClick(OnlineAutBean.DataBean item) {

        Intent intent = new Intent(this, OnlineAdActivity.class);
        intent.putExtra("orderId", item.getOut_order_no());
        startActivity(intent);

    }


}
